package com.kaiwukj.android.ufamily.mvp.ui.page.account.community;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.di.scope.ActivityScope;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.app.MyApplication;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.UserResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.account.AccountContract;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

@ActivityScope
public class CommunityPresenter extends BasePresenter<AccountContract.Model, AccountContract.CommunityView> {


    public static final int UPDATE = 3;

    @Inject
    RxErrorHandler rxErrorHandler;

    @Inject
    public CommunityPresenter(AccountContract.CommunityView view) {
        super(view);
    }

    public void fetchNearCommunity(double lat, double lon) {
        mModel.fetchNearCommunity(lat, lon)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<CommunityResult>>(rxErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<CommunityResult> result) {
                        mRootView.onFetchNearCommunity(result.getList());
                    }
                });
    }

    public void fetchCommunityByKeyword(String keyword) {
        mModel.fetchCommunityByKeyword(keyword)
                .doOnSubscribe(act -> mRootView.showLoading("正在搜索..."))
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<CommunityResult>>() {
                    @Override
                    public void onSuccess(@NonNull ListResp<CommunityResult> result) {
                        mRootView.onFetchNearCommunity(result.getList());
                    }
                });
    }

    public void updateCommunity(int communityId, String communityName) {
        mModel.updateCommunity(communityId)
                .doOnSubscribe(act -> mRootView.showLoading("正在更新..."))
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(rxErrorHandler, mRootView, UPDATE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        //在onTaskOk处理
                        UserResult userResult = MyApplication.getInstance().getUserInfo();
                        userResult.setLastCommunityId(communityId);
                        userResult.setLastCommunityName(communityName);
                        MyApplication.getInstance().setUserInfo(userResult);
                    }

                });
    }

}
